package com.yuan.zs.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.yuan.base.framework.BaseFrameworkActivity;
import com.yuan.base.utils.AppUtil;
import com.yuan.zs.R;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends BaseFrameworkActivity {

	private ViewPager viewPaper;
	private int[] imgs = {R.drawable.bg_start_image1, R.drawable.bg_start_image2, R.drawable.bg_start_image3};

	@Override
	protected int getLayoutId() {
		return R.layout.activity_guide;
	}

	@Override
	protected void bindViews() {
		viewPaper = findViewById(R.id.viewPaper);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ArrayList<View> views = new ArrayList<>();
		for (int i = 0; i < imgs.length; i++) {
			View view = getInflater().inflate(R.layout.item_guide, null);
			ImageView img = view.findViewById(R.id.iv_guide);
			img.setImageResource(imgs[i]);
			if (i == imgs.length - 1) {
				img.setOnClickListener(v -> {
					AppUtil.setFirstUse(false);
					Intent intent = new Intent();
					intent.setClass(GuideActivity.this, LoginActivity.class);
					startActivity(intent);
					closeSelf();
				});
			}
			views.add(img);
		}
		viewPaper.setAdapter(new GuidePagerAdapter(views));
	}

	private class GuidePagerAdapter extends PagerAdapter {

		private List<View> views;

		GuidePagerAdapter(List<View> views) {
			this.views = views;
		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(views.get(position));
			return views.get(position);
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(views.get(position));
		}
	}
}

